#include "stdafx.h"
#include "regedit.h"

namespace shared
{
	bool GetRegEditValue(HKEY hRoot, const std::wstring& url, const std::wstring& key, std::wstring& out_value, DWORD access/* = 0*/)
	{
		HKEY  hKeyResult = NULL;
		DWORD dwSize = 0;
		DWORD dwDataType = 0;
		bool ret = false;
		if (ERROR_SUCCESS == ::RegOpenKeyEx(hRoot, url.c_str(), 0, KEY_QUERY_VALUE | access, &hKeyResult))
		{
			if (ERROR_SUCCESS == ::RegQueryValueEx(hKeyResult, key.c_str(), 0, &dwDataType, NULL, &dwSize))
			{
				switch (dwDataType)
				{
				case REG_MULTI_SZ:
				{
									
									 BYTE* lpValue = new BYTE[dwSize];
									 memset(lpValue, 0, dwSize * sizeof(wchar_t));
									
									 LONG lRet = ::RegQueryValueEx(hKeyResult, key.c_str(), 0, &dwDataType, lpValue, &dwSize);
									 if (lRet == ERROR_SUCCESS)
									 {
										 //out_value = lpValue;
									 }
									 delete[] lpValue;
									 break;
				}
				case REG_SZ:
				{
							  
							   wchar_t* lpValue = new wchar_t[dwSize];
							   memset(lpValue, 0, dwSize * sizeof(wchar_t));
							  
							   if (ERROR_SUCCESS == ::RegQueryValueEx(hKeyResult, key.c_str(), 0, &dwDataType, (LPBYTE)lpValue, &dwSize))
							   {
								   out_value = lpValue;
								   ret = true;
							   }
							   delete[] lpValue;
							  
							   break;
				}
				default:
					break;
				}
			}
			::RegCloseKey(hKeyResult);
		}
		return ret;
	}
}